package com.asiainfo_linkage.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.pager.Pager;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.asiainfo_linkage.model.AddGroup;
import com.asiainfo_linkage.model.AddMember;
import com.asiainfo_linkage.model.TaskQuery;
import com.asiainfo_linkage.service.GroupQueryService;
import com.asiainfo_linkage.service.MemberQueryService;
import com.asiainfo_linkage.service.RemQueryService;
import com.asiainfo_linkage.vo.GroupQuery;


@IocBean
@InjectName
public class GroupManage {
	
	@Inject
	private GroupQueryService  groupQueryService;
	@Inject
	private MemberQueryService  memberQueryService;
	@Inject
	private RemQueryService  remQueryService;
	
	
	@At("/groupIndex")
	@Ok(value = "jsp:groupmanage.create") //组管理及组列表初始页面
	public Map<String, Object> listIndex(@Param("::groupQuery.") GroupQuery groupQuery, @Param("::pager.") Pager pager) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<AddGroup> list = groupQueryService.pageQuery(groupQuery, pager);
		map.put("list", list);
		map.put("pager", pager);
		map.put("groupQuery", groupQuery);
		return map;
	}
	
	@At("/memberquery")
	@Ok(value = "jsp:groupmanage.create") //组员查询页面
	public Map<String, Object> listIndex(@Param("::addGroup.")  AddGroup addGroup, @Param("::pager.") Pager pager) {
		
		Map<String, Object> map = new HashMap<String, Object>();//选择组查询组员
		AddMember memberQuery=new AddMember();
		memberQuery.setGROUP_ID(addGroup.getGROUP_ID());
		List<AddMember> list = memberQueryService.pageQuery(memberQuery, pager);
		map.put("memberlist", list);
		map.put("pager", pager);
		map.put("memberQuery", memberQuery);
		
		GroupQuery groupQuery=new GroupQuery();
		List<AddGroup> listgroup = groupQueryService.pageQuery(groupQuery, pager);
		map.put("list", listgroup);
//		map.put("gId", addGroup.getGROUP_ID());
		return map;
	}
	
	@At("/addGroup")
	@Ok(value = "jsp:groupmanage.newgroup") //新建组
    public void addGroup(){
	}

	/**
	 * 保存添加数据页面,保存完毕后跳转到列表页面。
	 */
	@At("/saveGroup")
	@Ok(value = "redirect:/groupIndex")
	public void save(@Param("::AddGroup.") AddGroup AddGroup) {
			//存储数据
		AddGroup.setGROUP_NAME(AddGroup.getGROUP_NAME());
		this.groupQueryService.dao().insert(AddGroup);
	}
	
	/**
	 * 增加单个组员。
	 */
	@At("/addMember")
	@Ok(value = "jsp:groupmanage.addmember") //导入新建组员
    public AddMember  addMember(@Param("::addMember.") AddMember addmember){
		
		return addmember;
	}
	
	/**
	 * 保存添加数据页面,保存完毕后跳转到分组列表页面。
	 */

	@At("/savemember")
	@Ok(value = "redirect:/groupIndex")
	public void save(@Param("::AddMember.") AddMember AddMember) {
		//存储数据
	AddMember.setGROUP_ID(AddMember.getGROUP_ID());
	AddMember.setMEMBER_NAME(AddMember.getMEMBER_NAME());
	AddMember.setMEMBER_EMAIL(AddMember.getMEMBER_EMAIL());
	AddMember.setMEMBER_PHONE(AddMember.getMEMBER_PHONE());
		this.memberQueryService.dao().insert(AddMember);
	}
	
	
	
	
	
	/**
	 * 保存编辑数据页面,保存完毕后跳转到列表页面。
	 */
	@At("/memberedit")
	@Ok(value = "jsp:groupmanage.memberedit") //跳转组员编辑页面
	public AddMember updatepage(@Param("::addMember") AddMember addMember) {
		List<AddMember> list = memberQueryService.pageQueryMemberId(addMember, null);
		addMember=list.get(0);
		return addMember;
	}
	
	/**
	 * 保存编辑数据页面,保存完毕后跳转到组员查询页面。
	 */
	@At("/updatemember")
	@Ok(value = "jsp:groupmanage.create") //修改成功返回组员查询页面
	public Map<String, Object> update(@Param("::addMember") AddMember addMember, @Param("::pager.") Pager pager) {
		
		this.memberQueryService.dao().update(addMember);
		
		Map<String, Object> map = new HashMap<String, Object>();
		AddMember memberQuery=new AddMember();
		memberQuery.setGROUP_ID(addMember.getGROUP_ID());
		List<AddMember> list = memberQueryService.pageQuery(memberQuery, pager);
		map.put("memberlist", list);
		map.put("pager", pager);
		map.put("memberQuery", memberQuery);
		
		GroupQuery groupQuery=new GroupQuery();
		List<AddGroup> listgroup = groupQueryService.pageQuery(groupQuery, pager);
		map.put("list", listgroup);
		map.put("gId", addMember.getGROUP_ID());
		return map;
	}


	
	/**
	 * 删除组员，删除完毕后跳转组员管理页面。
	 */
	@At("/deletemember")
	@Ok(value = "jsp:groupmanage.create") //修改成功返回组员查询页面
	public Map<String, Object> delete(@Param("::addMember") AddMember addMember, @Param("::pager.") Pager pager) {
		
		this.memberQueryService.dao().delete(addMember);
		
		Map<String, Object> map = new HashMap<String, Object>();
		AddMember memberQuery=new AddMember();
		memberQuery.setGROUP_ID(addMember.getGROUP_ID());
		List<AddMember> list = memberQueryService.pageQuery(memberQuery, pager);
		map.put("memberlist", list);
		map.put("pager", pager);
		map.put("memberQuery", memberQuery);
		
		GroupQuery groupQuery=new GroupQuery();
		List<AddGroup> listgroup = groupQueryService.pageQuery(groupQuery, pager);
		map.put("list", listgroup);
		map.put("gId", addMember.getGROUP_ID());
		return map;
	}
	
	
	
	/**
	 * 任务查询信息页面。
	 */
	@At("/taskquery")
	@Ok(value = "jsp:groupmanage.taskquery")//跳转查询页面
	public void querypage(@Param("::taskquery.")  TaskQuery taskquery, @Param("::pager.") Pager pager) {
	}
	
	
	/**
	 * 任务查询信息页面。
	 */
	@At("/taskqueryself")
	@Ok(value = "jsp:groupmanage.taskquery")  //根据查询条件展示结果
	public Map<String, Object> listIndex(@Param("::taskquery.")  TaskQuery taskquery, @Param("::pager.") Pager pager) {
		
		List<Map> list = remQueryService.pageQuerySelf(taskquery);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("tasklist", list);
		map.put("pager", pager);
		map.put("taskquery", taskquery);
		return map;
	}
	
	
	
	/**
	 * 详情页面。
	 */
	@SuppressWarnings("rawtypes")
	@At("/taskdetail")
	@Ok(value = "jsp:groupmanage.taskdetail") 
	public Map<String, Object> listIndextail(@Param("::taskquery.")  TaskQuery taskquery, @Param("::pager.") Pager pager) {
	
	List<Map> list = remQueryService.pageQueryDetail(taskquery);
	Map<String, Object> map=new HashMap<String, Object>();
	map.put("tasklist", list);
	map.put("pager", pager);
	map.put("taskquery", taskquery);
	return map;
}

	
	
}
